import { _decorator, Component, error, EventTouch, Node } from 'cc';
import { EventManager, MyEvent } from './EventManager';
import { Rotate } from './Rotate';
const { ccclass, property } = _decorator;

@ccclass('RotatePanel')
export class RotatePanel extends Component {

    isTouch: boolean = false;

    protected onLoad(): void {
        this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
    }

    onTouchStart(event: EventTouch) {
        this.isTouch = true;
        Rotate.Instance.isClick = true;
    }
    onTouchMove(event: EventTouch) {
        if (this.isTouch) {
            const prePosi = event.getPreviousLocation();
            const currentMousePosition = event.getLocation()
            const delta = currentMousePosition.subtract(prePosi);
            EventManager.Scene.emit(MyEvent.ROTATE, delta);
        }
    }

    onTouchEnd(event: EventTouch) {
        this.isTouch = false;
        Rotate.Instance.isClick = false;
    }
}


